首页>代码>30个java常用工具类分享>/[工具类] 文件FileUtil.java
package com.common.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.Iterator;

import javax.swing.text.html.HTMLDocument.HTMLReader.FormAction;

/**
 * 
 * 功能描述:
 * 
 * @author Administrator
 * @Date Jul 19, 2008
 * @Time 9:46:11 AM
 * @version 1.0
 */
public class FileUtil {

	/**
	 * 功能描述:列出某文件夹及其子文件夹下面的文件,并可根据扩展名过滤
	 * 
	 * @param path
	 *            文件夹
	 */
	public static void list(File path) {
		if (!path.exists()) {
			System.out.println("文件名称不存在!");
		} else {
			if (path.isFile()) {
				if (path.getName().toLowerCase().endsWith(".pdf")
						|| path.getName().toLowerCase().endsWith(".doc")
						|| path.getName().toLowerCase().endsWith(".chm")
						|| path.getName().toLowerCase().endsWith(".html")
						|| path.getName().toLowerCase().endsWith(".htm")) {// 文件格式
					System.out.println(path);
					System.out.println(path.getName());
				}
			} else {
				File[] files = path.listFiles();
				for (int i = 0; i < files.length; i++) {
					list(files[i]);
				}
			}
		}
	}

	/**
	 * 功能描述:拷贝一个目录或者文件到指定路径下,即把源文件拷贝到目标文件路径下
	 * 
	 * @param source
	 *            源文件
	 * @param target
	 *            目标文件路径
	 * @return void
	 */
	public static void copy(File source, File target) {
		File tarpath = new File(target, source.getName());
		if (source.isDirectory()) {
			tarpath.mkdir();
			File[] dir = source.listFiles();
			for (int i = 0; i < dir.length; i++) {
				copy(dir[i], tarpath);
			}
		} else {
			try {
				InputStream is = new FileInputStream(source); // 用于读取文件的原始字节流
				OutputStream os = new FileOutputStream(tarpath); // 用于写入文件的原始字节的流
				byte[] buf = new byte[1024];// 存储读取数据的缓冲区大小
				int len = 0;
				while ((len = is.read(buf)) != -1) {
					os.write(buf, 0, len);
				}
				is.close();
				os.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		File file = new File("D:\\个人资料\\MySQL 5");
//		list(file);
		Date myDate = new Date(); 
		DateFormat df = DateFormat.getDateInstance();
		System.out.println(df.format(myDate)); 
	}

}
最近下载更多
ljt289917726  LV3 2022年9月5日
zhy1989wz  LV6 2022年3月11日
wxhky159159  LV1 2022年1月4日
duqiangedu  LV3 2021年12月16日
LanQian111111  LV1 2021年8月18日
落后就要挨打  LV26 2021年6月16日
耀眼的星星  LV3 2021年4月17日
2469095052  LV8 2021年3月3日
花椒谢霆锋  LV8 2021年3月3日
gaoyangzhi  LV1 2021年1月25日
最近浏览更多
lee123321  LV22 2023年12月19日
szf123  LV12 2023年5月31日
浪里格朗  LV4 2023年1月31日
二进制2  LV3 2023年1月6日
jjfskldjf  LV2 2022年11月27日
11220011  LV5 2022年11月7日
香菇肉饼汤  LV8 2022年10月30日
heqian  LV16 2022年10月14日
爱情戴罪的羔羊  LV7 2022年9月15日
ljt289917726  LV3 2022年9月5日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友